package com.globalwave.pos.inventory;



import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.globalwave.pos.inventory.entity.InventoryBalance;
import com.globalwave.pos.inventory.entity.InventoryPeriod;
import com.globalwave.pos.inventory.entity.InventoryPeriodBalance;
import com.globalwave.pos.inventory.entity.InventoryTransaction;
import com.globalwave.pos.inventory.service.InventoryPeriodBalanceBO;


import static com.globalwave.pos.inventory.entity.InventoryPeriod.*;


public class InventoryPeriodBalanceBOTest extends InventoryBaseTestCase {
	

	
	@Test
	public void snapshot_case4() throws Exception {
		
		execute(getSqlFile(InventoryPeriodBOTest.class, "closeAndOpenNext_case2")) ;
		
		InventoryBalance balanceExpected = new InventoryBalance() ;
		
		//execute(getSqlFile(this.getClass(), "period")) ;
		InventoryBalance balance = getBalance(warehouseCode, stockItemNum1);
		
		balanceExpected.setOn_hand_quantity(100) ; 
		balanceExpected.setMortgaged_quantity(-7) ;
		
		assertBalance("init data ", balanceExpected, balance) ; 
		
		balance = getBalance(warehouseCode, stockItemNum2);
			
		balanceExpected.setOn_hand_quantity(200) ; 
		balanceExpected.setMortgaged_quantity(-6) ;

		assertBalance("init data ", balanceExpected, balance) ; 
			
		balance = getBalance(warehouseCode2, stockItemNum1);
			
		balanceExpected.setOn_hand_quantity(300) ; 
		balanceExpected.setMortgaged_quantity(-5) ;

		assertBalance("init data ", balanceExpected, balance) ; 
		
        // ------------------------------ 
		getInventoryPeriodBalanceBO().snapshot() ;

        // ------------------------------ 
		
        balance = getBalance(warehouseCode, stockItemNum1);
		
		balanceExpected.setOn_hand_quantity(100) ; 
		balanceExpected.setMortgaged_quantity(-7) ;
		
		assertBalance("after snapshot ", balanceExpected, balance) ; 
		
		balance = getBalance(warehouseCode, stockItemNum2);
			
		balanceExpected.setOn_hand_quantity(200) ; 
		balanceExpected.setMortgaged_quantity(-6) ;

		assertBalance("after snapshot ", balanceExpected, balance) ; 
			
		balance = getBalance(warehouseCode2, stockItemNum1);
			
		balanceExpected.setOn_hand_quantity(300) ; 
		balanceExpected.setMortgaged_quantity(-5) ;

		assertBalance("after snapshot ", balanceExpected, balance) ; 
		

		InventoryPeriodBalance periodBalanceExpected = new InventoryPeriodBalance() ;
		
		InventoryPeriodBalance periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(100) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;

        periodBalance = getPeriodBalance(warehouseCode, stockItemNum2, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(200) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;
		
        periodBalance = getPeriodBalance(warehouseCode2, stockItemNum1, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(300) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;

		
        periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201108) ;
		
        Assert.assertNull("after snapshot 201108 must be Null", periodBalance) ;
        

        periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201110) ;
		
        Assert.assertNull("after snapshot 201110 must be Null", periodBalance) ;
        


        assertInventoryPeriodPO_case4_5();
	}
	
	private void assertInventoryPeriodPO_case4_5() throws Exception {

        InventoryPeriod inventoryPeriod = getPeriod(201108L) ;
    	Assert.assertEquals("after close/open 201108 - status, ", INVENTORY_PERIOD_STATUS_CLOSE, inventoryPeriod.getStatus_()) ;
    	Assert.assertEquals("after close/open 201108 - start,  ", dateFormat.parseObject("20110801"), inventoryPeriod.getDate_from()) ;
    	Assert.assertEquals("after close/open 201108 - end,    ", dateFormat.parseObject("20110831"), inventoryPeriod.getDate_to()) ;

    	inventoryPeriod = getPeriod(201109L) ;
     	Assert.assertEquals("after close/open 201109 - status, ", INVENTORY_PERIOD_STATUS_CLOSE, inventoryPeriod.getStatus_()) ;
    	Assert.assertEquals("after close/open 201109 - start,  ", dateFormat.parseObject("20110901"), inventoryPeriod.getDate_from()) ;
    	Assert.assertEquals("after close/open 201109 - end,    ", dateFormat.parseObject("20110930"), inventoryPeriod.getDate_to()) ;
     	
     	inventoryPeriod = getPeriod(201110L) ;
     	Assert.assertEquals("after close/open 201110 - status, ", INVENTORY_PERIOD_STATUS_OPEN, inventoryPeriod.getStatus_()) ;
    	Assert.assertEquals("after close/open 201110 - start,  ", dateFormat.parseObject("20111001"), inventoryPeriod.getDate_from()) ;
    	Assert.assertEquals("after close/open 201110 - end,    ", dateFormat.parseObject("20111031"), inventoryPeriod.getDate_to()) ;
		
	}

	@Test
	public void snapshot_case5() throws Exception {

		execute(getSqlFile(this.getClass(), "snapshot_case4")) ;
		execute(getSqlFile(InventoryPeriodBOTest.class, "closeAndOpenNext_case1")) ;
		
				
        // ------------------------------ 
		getInventoryPeriodBalanceBO().snapshot() ;

        // ------------------------------ 
		
		InventoryBalance balance = getBalance(warehouseCode, stockItemNum1);
		
		InventoryBalance balanceExpected = new InventoryBalance() ;

		balanceExpected.setOn_hand_quantity(100) ; 
		balanceExpected.setMortgaged_quantity(-12) ;
		
		assertBalance("after snapshot ", balanceExpected, balance) ; 
		
		balance = getBalance(warehouseCode, stockItemNum2);
			
		balanceExpected.setOn_hand_quantity(200) ; 
		balanceExpected.setMortgaged_quantity(-6) ;

		assertBalance("after snapshot ", balanceExpected, balance) ; 
			
		balance = getBalance(warehouseCode2, stockItemNum1);
			
		balanceExpected.setOn_hand_quantity(300) ; 
		balanceExpected.setMortgaged_quantity(-5) ;

		assertBalance("after snapshot ", balanceExpected, balance) ; 
		

		InventoryPeriodBalance periodBalanceExpected = new InventoryPeriodBalance() ;
		
		InventoryPeriodBalance periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(100) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;

        periodBalance = getPeriodBalance(warehouseCode, stockItemNum2, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(200) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;
		
        periodBalance = getPeriodBalance(warehouseCode2, stockItemNum1, 201109) ;
		
		periodBalanceExpected.setOn_hand_quantity(300) ;
		assertPeriodBalance("after snapshot ", periodBalanceExpected, periodBalance) ;

		
        periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201108) ;
		
        Assert.assertNull("after snapshot 201108 must be Null", periodBalance) ;
        

        periodBalance = getPeriodBalance(warehouseCode, stockItemNum1, 201110) ;
		
        Assert.assertNull("after snapshot 201110 must be Null", periodBalance) ;
        
        Assert.assertNull("after snapshot trans must be Null", getTempTransaction(warehouseCode, stockItemNum1)) ;

        assertInventoryPeriodPO_case4_5();
		
	}
	
	
	@Test
	public void fix_case7() throws Exception {

		execute(getSqlFile(this.getClass(), "snapshot_case4")) ;
		execute(getSqlFile(InventoryPeriodBOTest.class, "closeAndOpenNext_case1")) ;
		

		Assert.assertNull("before fix", getPeriodBalance(warehouseCode2, stockItemNum2, 201108));
		
        // ------------------------------ 
		getInventoryPeriodBalanceBO().fix() ;

        // ------------------------------ 

		InventoryBalance balanceExpected = new InventoryBalance() ;
		InventoryBalance balance = getBalance(warehouseCode, stockItemNum1);
			
		balanceExpected.setOn_hand_quantity(114) ; 
		balanceExpected.setMortgaged_quantity(-7) ;

		assertBalance("after fix ", balanceExpected, balance) ; 


		balance = getBalance(warehouseCode, stockItemNum2);
			
		balanceExpected.setOn_hand_quantity(210) ; 
		balanceExpected.setMortgaged_quantity(-6) ;

		assertBalance("after fix ", balanceExpected, balance) ;

		

		balance = getBalance(warehouseCode2, stockItemNum1);
			
		balanceExpected.setOn_hand_quantity(303) ; 
		balanceExpected.setMortgaged_quantity(-10) ;

		assertBalance("after fix ", balanceExpected, balance) ;
		
		
		Assert.assertEquals("check period onhand", 95, getPeriodBalance(warehouseCode,  stockItemNum1, 201106).getOn_hand_quantity().intValue());
		Assert.assertNull(  "check period onhand",     getPeriodBalance(warehouseCode,  stockItemNum2, 201106));
		Assert.assertEquals("check period onhand", 88, getPeriodBalance(warehouseCode2, stockItemNum1, 201106).getOn_hand_quantity().intValue());
		Assert.assertEquals("check period onhand", 99, getPeriodBalance(warehouseCode,  stockItemNum1, 201107).getOn_hand_quantity().intValue());
		Assert.assertEquals("check period onhand", 67, getPeriodBalance(warehouseCode2, stockItemNum1, 201107).getOn_hand_quantity().intValue());
		Assert.assertNull(  "check period onhand",     getPeriodBalance(warehouseCode2, stockItemNum2, 201107));
		Assert.assertEquals("check period onhand", 99, getPeriodBalance(warehouseCode,  stockItemNum1, 201108).getOn_hand_quantity().intValue());
		Assert.assertEquals("check period onhand",100, getPeriodBalance(warehouseCode,  stockItemNum2, 201108).getOn_hand_quantity().intValue());
		Assert.assertEquals("check period onhand", 85, getPeriodBalance(warehouseCode2, stockItemNum1, 201108).getOn_hand_quantity().intValue());
		Assert.assertEquals("check period onhand", 10, getPeriodBalance(warehouseCode2, stockItemNum2, 201108).getOn_hand_quantity().intValue());
		

		List<Object> ts =(List<Object>)query(
				    InventoryTransaction.class, 
					new String[]{"warehouseCode in('" + warehouseCode + "', '" + warehouseCode + "')"}) ;
		
		
		Assert.assertEquals("No InventoryTemporaryTransactionPO", 0, ts.size()) ;
		
		

        Assert.assertEquals("after close/open 201108 - status, ", INVENTORY_PERIOD_STATUS_CLOSE, getPeriod(201108L).getStatus_()) ;
    	
    	Assert.assertEquals("after close/open 201109 - status, ", INVENTORY_PERIOD_STATUS_OPEN, getPeriod(201109L).getStatus_()) ;
    	
     	Assert.assertEquals("after close/open 201110 - status, ", INVENTORY_PERIOD_STATUS_NOT_OPEN, getPeriod(201110L).getStatus_()) ;
    	
	}
	
	private InventoryPeriodBalanceBO getInventoryPeriodBalanceBO () {
		return new InventoryPeriodBalanceBO() ;
	}
}
